package cn.tedu.charging.device.service.impl;

import cn.tedu.charging.common.pojo.query.NearStationsQuery;
import cn.tedu.charging.common.pojo.vo.GunInfoVO;
import cn.tedu.charging.common.pojo.vo.StationDetailVO;
import cn.tedu.charging.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.device.dao.repository.DeviceRepository;
import cn.tedu.charging.device.pojo.po.ChargingGunInfoPO;
import cn.tedu.charging.device.pojo.po.ChargingStationPO;
import cn.tedu.charging.device.pojo.po.StationGeoPO;
import cn.tedu.charging.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceRepository deviceRepository;


    @Override
    public void StartOnceWarmUp() {
        //TODO
        Boolean exists = deviceRepository.geoStationsExists();

        if (!exists) {
            Long total = deviceRepository.countAll();
            Long batch = 10000L;
            Long loops = total % batch == 0 ? total / batch : total / batch + 1;

            for (Long item = 0l; item < loops; item++) {
                log.debug("循环第{}次", item + 1);
                Long start = batch * item;
                Long rows = batch;
                List<ChargingStationPO> pos = deviceRepository.getStationsPage(start, rows);
                deviceRepository.saveGeos(pos);
            }
        }
//        List<ChargingStationPO> pos = deviceRepository.getStations();
//        if(pos != null && pos.size() > 0){
//            deviceRepository.saveGeos(pos);
//        }
    }

    @Override
    public List<StationInfoVO> nearStations(NearStationsQuery query) {
        List<StationInfoVO> listVo = new ArrayList<>();
        //
        log.debug("开始查询附近充电站...");
        List<StationGeoPO> listPO = deviceRepository.nearStations(query);

        if(listPO != null && listPO.size() > 0){
            log.debug("查询到{}个场站",listPO.size());
//            listVo = listPO.stream().map(po -> {
//                StationInfoVO vo = new StationInfoVO();
//                BeanUtils.copyProperties(po,vo);
//                vo.setAvgPrice(calculateAvgPrice(po.getStationId()));
//                return vo;
//            }).collect(Collectors.toList());
            for(Integer item = 0;item<listPO.size();item++){
                StationInfoVO vo = new StationInfoVO();
                StationGeoPO po = listPO.get(item);
                BeanUtils.copyProperties(po,vo);
                vo.setAvgPrice(calculateAvgPrice(po.getStationId()));
                listVo.add(vo);
                //排序
                listVo.sort((o1, o2) -> o1.getDistance().compareTo(o2.getDistance()));
            }

        }else{
            log.debug("未命中任何场站数据...");
        }

        return listVo;
    }

    private Double calculateAvgPrice(Integer stationId) {
        return 1.55;
    }

    @Override
    public StationDetailVO detailStation(Integer stationId) {
        log.debug("开始查询充电站详情,stationId:{}",stationId);
        //准备返回值
        StationDetailVO detailVO = new StationDetailVO();

        ChargingStationPO po = deviceRepository.getStationById(stationId);

        if(po != null){
            log.debug("查询场站信息,命中stationPO:{}",po);
            BeanUtils.copyProperties(po,detailVO);
            detailVO.setStationId(stationId);
            List<ChargingGunInfoPO> guns = deviceRepository.getStationGuns(stationId);
            if(guns != null && guns.size() > 0){
                log.debug("查询场站 guns:{}",guns);
                detailVO.setGunInfoVos(guns.stream().map(gun -> {
                    GunInfoVO vo = new GunInfoVO();
                    BeanUtils.copyProperties(gun,vo);
                    return vo;
                }).collect(Collectors.toList()));
            }



        }else {
            log.debug("未命中场站信息...");
        }

        return detailVO;
    }

    @Override
    public void updateGunStatus(Integer gunId, Integer status) {

    }

    @Override
    public Boolean checkGunAvailable(Integer gunId) {
        return null;
    }
}































